﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GuiUtilities
{
    public partial class FrmExceptionNotifier : DevExpress.XtraEditors.XtraForm
    {
        static DateTime _TimeFlag;
        static int _SecondsForLife = -1;
        static bool _UseAutoCloseForm = false;
        private FrmExceptionNotifier(string exceptionDescription, string exceptionPosition)
        {
            InitializeComponent();
            this.AcceptButton = btnClose;
            txtExceptionDescription.Text = exceptionDescription + "\n";
            txtExceptionPosition.Text = exceptionPosition;
            AccountingLibs.CustomException.WriteLog(string.Format("Lỗi \"{0}\" tại {1}\n-----------------------------------------------------\n",
                exceptionDescription, exceptionPosition));
        }
        private FrmExceptionNotifier(string exceptionDescription, string exceptionPosition, int timeForLife)
        {
            InitializeComponent();
            this.AcceptButton = btnClose;
            txtExceptionDescription.Text = exceptionDescription + "\n";
            txtExceptionPosition.Text = exceptionPosition;
            AccountingLibs.CustomException.WriteLog(string.Format("Lỗi \"{0}\" tại {1}\n-----------------------------------------------------\n",
                exceptionDescription, exceptionPosition));
            _TimeFlag = DateTime.Now;
            _SecondsForLife = timeForLife;
        }

        public static void ShowAndLog(Exception ex)
        {
            try
            {
                FrmExceptionNotifier frm = new FrmExceptionNotifier(ex.Message, ex.StackTrace);
                frm.ShowDialog();
            }
            catch (Exception exx)
            {
                MessageBox.Show("Lỗi ShowAndLog : " + exx.Message);
            }
        }
        public static void ShowAndLog(Exception ex, bool useTimeForLife, int timeForLife)
        {
            _UseAutoCloseForm = useTimeForLife;
            try
            {
                if (_UseAutoCloseForm)
                {
                    FrmExceptionNotifier frm = new FrmExceptionNotifier(ex.Message, ex.StackTrace, timeForLife);
                    frm.ShowDialog();
                }
                else
	            {
                    FrmExceptionNotifier frm = new FrmExceptionNotifier(ex.Message, ex.StackTrace);
                    frm.ShowDialog();
	            }
            }
            catch (Exception exx)
            {
                MessageBox.Show("Lỗi ShowAndLog : " + exx.Message);
            }
        }

        private void FrmExceptionNotifier_Load(object sender, EventArgs e)
        {
            timerForLife.Interval = 100;
            timerForLife.Start();
        }
        public void timerForLife_Tick(object sender, EventArgs e)
        {
            TimeSpan timeSpan = DateTime.Now - _TimeFlag;
            if (timeSpan.Seconds >= _SecondsForLife && _UseAutoCloseForm)
            {
                btnClose_Click(null, null);
                timerForLife.Stop();
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Dispose();
        }
        private void btnQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
